<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use think\Cookie;
use app\admin\model\ConfigModel;
use app\admin\model\MealModel;
use app\admin\model\AdminlogModel;
use app\admin\model\Rule as RuleModel;
class Common extends Controller
{ 
    public function _initialize()
    {
        if(!Session::has('admin')){
            return $this->redirect('admin/admin/login');   //跳转到登录页
        }
        if(request()->isPost()){

            $post= input('post.');
            // dump($post);
            if($post){
                $this->_think_log();
            }
        }
        $this->admin_info=Session::get('admin');
        $this->assign('think_admin', $this->admin_info);
    }
    //获取地区子类
    public function getzi(){
        $pid=input('get.pid');
        $list=Db::table('area')->where('Parent_code',$pid)->select();
        echo json_encode($list);
    }
    //获取地区子类
    public function gettypezi(){
        $pid=input('get.pid');
        $list=Db::table('type')->where('pid',$pid)->select();
        echo json_encode($list);
    }
    // public function getMeal(){
    //     $get=input('get.');
    //     $meal=MealModel::where('product_id',$get['product_id'])->select();
    //     return json_encode($meal,320);
    // }
    //日志文件
    public function _think_log(){
        $post= input('post.');
        $module = request()->module();
        $action = request()->action();
        $admin=Session::get('admin');
        $admin_id=$admin['id'];
        $controller = strtolower(request()->controller());
        $RuleModel=new RuleModel;
        $where['controller']=['like','%'.$controller.'%'];
        $rule_info=$RuleModel->where($where)->find();
        // dump($rule_info);die;
        $arr=[
            'add'=>'添加',
            'do_add'=>'添加',
            'save'=>'恢复',
            'update'=>'修改',
            'do_update'=>'修改',
            'del'=>'删除',
            'do_del'=>'删除',
            'del_all'=>'删除',
            'del_list'=>'删除',
            'up_status'=>'状态变更',
            'shen'=>'审核',
            'set'=>'操作金额',
            'sub'=>'提交审核',
            'lst'=>'获取列表',
            'water_lst'=>'获取列表',
            'up_sort'=>'排序',
            'info'=>'个人信息编辑',
        ];
        // dump($rule_info);die;
        if($rule_info){
            if(isset($arr[$action])){
                $data['save']=$arr[$action];
                $data['title']=$rule_info['rule_name'];
                $data['type_is']=$module=='admin'?'1':'2';
                $data['ip']=get_real_ip();
                $data['createtime']=time();
                $data['a_id']=$admin_id;
                $data['com']=json_encode($post,320);
                // dump($data);die;
                $AdminlogModel= new AdminlogModel;
                $AdminlogModel->allowField(true)->save($data);
                 // print_r($rule_info->toArray());
            }
            
        }
    }
  

    public function yan1()
    { 
      
      //1.创建黑色画布
        $image = imagecreatetruecolor(100, 30);
         
        //2.为画布定义(背景)颜色
        $bgcolor = imagecolorallocate($image, 255, 255, 255);
         
        //3.填充颜色
        imagefill($image, 0, 0, $bgcolor);
         
        // 4.设置验证码内容
         
        //4.1 定义验证码的内容
        $content = "0123456789";
         
        //4.1 创建一个变量存储产生的验证码数据，便于用户提交核对
        $captcha = "";
        for ($i = 0; $i < 4; $i++) {
            // 字体大小
            $fontsize = 100;
            // 字体颜色
            $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            // 设置字体内容
            $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
            $captcha .= $fontcontent;
            // 显示的坐标
            $x = ($i * 100 / 4) + mt_rand(5, 10);
            $y = mt_rand(5, 10);
            // 填充内容到画布中
            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
        }
        // $_SESSION["captcha"] = $captcha;
         session("admincaptcha",$captcha);
        //4.3 设置背景干扰元素
        for ($$i = 0; $i < 200; $i++) {
            $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
        }
         
        //4.4 设置干扰线
        for ($i = 0; $i < 3; $i++) {
            $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
        }
         
        //5.向浏览器输出图片头信息
        header('content-type:image/png');
         
        //6.输出图片到浏览器
        imagepng($image);
         
        //7.销毁图片
        imagedestroy($image);
    }

    //文件上传
    public function uploads($file,$path){
        $return=[];
            // 获取表单上传文件 例如上传了001.jpg
            // $file = request()->file('image');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                // $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'admin');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$path);
                if($info){
                    // 成功上传后 获取上传信息
                    $return['code']=0;
                    $return['msg']="上传成功";
                    $return['data']='/uploads/'.$path.'/'.str_replace('\\','/',$info->getSaveName());
                }else{
                    // 上传失败获取错误信息
                    $return['code']=1;
                    // $return['msg']="上传成功";
                    $return['msg']="上传失败";
                    $return['data']=$file->getError();
                }
            }
        return json_encode($return);
    }
    //多文件
    public function uploads_more($files,$path){

          foreach($files as $file){

            // 移动到框架应用根目录/public/uploads/ 目录下

            $bool=null;

            $path1="";

          if($file){

               $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$path);
              if($info){

                $bool=true;

                $path1.=$info->getSaveName();

            }else{

              $bool=false;

            }

          }

        }
        if($bool){

              $return['code']=0;

              $return['msg']="上传成功";

              $return['data']=$path1;

        }else{

              $return['code']=1;

              // $return['msg']="上传成功";

              $return['msg']="上传失败";

              $return['data']=$file->getError();

        }
        return $return;
    }
   
    
}
